//
//  YWMeViewController.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/15.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWMeViewController.h"
#import "YWHttpTool.h"
#import "YWMeHeaderView.h"
#import "YWLogin.h"
#import "YWUserTool.h"

static NSString *const kUserMessageNotification = @"kUserMessageNotification";

@interface YWMeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) YWWeChatModel *headModel;
@end

@implementation YWMeViewController
{
    UISwitch *_sw;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    __weak typeof(self) weakSelf = self;
    [YWUserTool isLogin:^(YWWeChatModel *mod) {
       
        weakSelf.headModel = mod;
        [weakSelf.tableView reloadData];
        
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieve:) name:kUserMessageNotification object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    
}

-(UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MAINWIDTH, MAINHEIGHT) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
#pragma mark - delegate and dataSource -

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    _sw = [[UISwitch alloc]initWithFrame:CGRectMake(MAINWIDTH-20-50, (40-30)/2.0, 50, 30)];
    [cell addSubview:_sw];
    [_sw addTarget:self action:@selector(switchSelect) forControlEvents:UIControlEventValueChanged];
    cell.imageView.image = [UIImage imageNamed:@"mine-moon-icon-click"];
    cell.textLabel.text = @"夜间模式";
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YWMeHeaderView *headerView = [YWMeHeaderView new];
    headerView.model = _headModel;
    headerView.wxLogin = ^{
    
        [YWLogin openWeChat:nil];
        
    };
    return section == 0? headerView:nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [YWMeHeaderView fixHeaderHeigh];
}

-(void)recieve:(NSNotification *)noti
{
    _headModel = noti.object;
    NSLog(@"%@",noti.object);
    [self.tableView reloadData];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)switchSelect
{
    _sw.selected = !_sw.selected;
    [[UIScreen mainScreen] setBrightness:_sw.selected?0.3:1];
}
@end
